package JavaServlet.TimeServlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Entity.PageBean;
import Service.IBanjiService;
import Service_Impl.BanjiServiceImpl;

@WebServlet("/banji")
public class BanjiServlet extends HttpServlet{
	
	private IBanjiService banjiService = new BanjiServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		
		if(method == null || "".equals(method)) {
			method = "pageList";
		}
		
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "toUpdate":
			toUpdate(req,resp);
			break;
		case "searchName":
			searchName(req,resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "pageList":
			pageList(req, resp);
			break;
		case "chooseBanji":
			chooseBanji(req, resp);
			break;
		default:
			System.out.println("default");
			break;
		}
	}

	private void chooseBanji(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		ArrayList<Banji> list = new ArrayList<Banji>();
		ResultSet resultSet = null;
		Connection connection = null;
		Statement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id, name from banji";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Banji banji = new Banji(id, name);
				list.add(banji);
			}
			for (Banji banji : list) {
				System.out.println(banji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		resp.sendRedirect("/JavaWeb190703/banji");
	}

	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		
		if(pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if(pageSize == null || "".equals(pageSize)) {
			pageSize = "3";
		}
		PageBean pageBean = banjiService.pageList(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageBean);
		req.setAttribute("pageBean", pageBean);
		req.getRequestDispatcher("banji_page_list.jsp").forward(req, resp);
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//		req.setCharacterEncoding("UTF-8");
		System.out.println("BanjiServlet.update()");
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		Banji banji = new Banji(Integer.parseInt(id), name);
		boolean isSuccess = banjiService.update(banji);
		
		resp.sendRedirect(req.getContextPath() + "/banji?method=selectAll");
	}

	private void searchName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("BanjiServlet.selectAll()");
		//得到前端数据
		String searchName = req.getParameter("searchName");
		//调用service层方法
		List<Banji>list = banjiService.searchName(searchName);
		
		
		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/banji_list.jsp").forward(req, resp);
	}

	private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		System.out.println("BanjiServlet.toUpdate()");
		String updateId = req.getParameter("id");
//		String name = req.getParameter("name");
		Banji banji = banjiService.selectById(Integer.parseInt(updateId));
		System.out.println(banji);
		
		
		// 放到一块内存区域里面
		req.setAttribute("banji", banji);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/banji_update.jsp").forward(req, resp);
	}
	
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("BanjiServlet.insert()");
//		req.setCharacterEncoding("UTF-8");
		String name = req.getParameter("name");
		Banji banji = new Banji(name);
		
		boolean isSuccess = banjiService.insert(banji);
		
//		resp.sendRedirect(req.getContextPath() + "/banji?metnod=selectAll");
		resp.sendRedirect(req.getContextPath() + "/banji");
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("BanjiServlet.deleteById()");
		String id = req.getParameter("id");
		
		boolean isSuccess = banjiService.deleteById(Integer.parseInt(id));
		// 删除完了之后重定向到列表界面再展示所有数据
		resp.sendRedirect(req.getContextPath() + "/banji?method=selectAll");
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("BanjiServlet.selectAll()");
		
		List<Banji>list = banjiService.selectAll();
		
		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/banji_list.jsp").forward(req, resp);
	}
}
